<?php
/**
 * YBatch.php
 *
 * @author Hua Yang <htmlcook@gmail.com>
 * @since 2.0
 */

Yii::import('common.components.queue.YDatabaseQueue');

/**
 * Class YBatch
 */
class YBatch extends YDatabaseYQueue
{
	/**
	 * @param int $leaseTime
	 * @return bool|mixed|object
	 */
	public function claimItem($leaseTime = 0)
	{
		$item = $this->db->createCommand()
			->select('data, item_id')
			->from('{{queue}}')
			->where('name=:name', array(':name' => $this->name))
			->order('item_id ASC')
			->limit(1, 0)
			->queryRow();
		if ($item) {
			$item['data'] = unserialize($item['data']);
			return (object) $item;
		}
		return false;
	}

	/**
	 * @return array|CDbDataReader
	 */
	public function getAllItems()
	{
		$items = $this->db->createCommand()
			->select('data')
			->from('{{queue}}')
			->where('name=:name', array(':name' => $this->name))
			->order('item_id ASC')
			->queryAll();
		foreach ($items as $key => $item) {
			$items[$key] = (object) $item;
			$item->data = unserialize($item->data);
		}
		return $items;
	}
} 